package ch.eureka.provider;

import ch.eureka.provider.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.*;

import java.util.logging.Logger;

/**
 * 入口
 */
@SpringBootApplication
@RestController
@EnableDiscoveryClient
@PropertySource("application.yml")
public class ProviderApplication
{
    private Logger logger = Logger.getLogger(this.getClass().getName());

    @Autowired
    private DiscoveryClient client;

    @Value(value = "${server.port}")
    public Integer port;

    @RequestMapping(value = "/hello")
    public String hello(){
        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("provider-service-host:" + instance.getHost() + ",service-id:" + instance.getServiceId());
        return "hello-eureka-provider,port:" + port;
    }

    /**
     * 单字符串参数接口
     * @param name
     * @return
     */
    @RequestMapping(value = "/hello1")
    public String hello1(@RequestParam String name){
        return "携带字符串参数:" + name;
    }

    /**
     * 双参数接口
     * 对于对象User必须要有默认构造函数，否则会报错
     * @param name
     * @param age
     * @return
     */
    @RequestMapping(value = "/hello2")
    public User hello2(@RequestHeader String name,
                       @RequestHeader Integer age){
        return new User(age, name);
    }

    /**
     * 对象参数接口
     * @param user
     * @return
     */
    @RequestMapping(value = "/hello3")
    public String hello3(@RequestBody User user){
        return user.getName() + " : " + user.getAge();
    }

    public static void main( String[] args )
    {
        SpringApplication.run(ProviderApplication.class);
    }
}
